package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址簿相关接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址")
    public Result insertAddress(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);
        addressBookService.insertAddress(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询用户所有地址")
    public Result<List<AddressBook>> allAddress(){
        log.info("查询用户所有地址");
        List<AddressBook> addressList = addressBookService.allAddress();
        return Result.success(addressList);
    }

    @GetMapping("/default")
    @ApiOperation("查询用户默认地址")
    public Result<AddressBook> defaultAddress(){
        log.info("查询用户默认地址");
        AddressBook addressBook = addressBookService.defaultAddress();
        return Result.success(addressBook);
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddress(Long id){
        log.info("根据id删除地址:{}",id);
        addressBookService.deleteAddress(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> addressById(@PathVariable Long id) {
        log.info("根据id查询地址:{}", id);
        AddressBook addressBook = addressBookService.addressById(id);
        return Result.success(addressBook);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址:{}", addressBook);
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();
    }
}
